<?php
namespace App\Services;

use Illuminate\Support\Facades\Redis;

class RedisService
{
    protected $redis_connect;

    protected $name = '';

    protected $prefix = '';

    public function __construct()
    {
        $this->redis_connect = Redis::connection($this->name);
    }

    /**
     * @return static
     */
    public static function getInstance()
    {
        static $instances = [];
        $class = get_called_class();

        if (!isset($instances[$class])) {
            $instances[$class] = new $class();
        }

        return $instances[$class];
    }

    protected function addPrefix($key)
    {
        return $this->prefix . $key;
    }

    public function set($key, $value, $expireResolution = null, $expireTTL = null, $flag = null)
    {
        return $this->redis_connect->set($this->addPrefix($key), $value, $expireResolution, $expireTTL, $flag);
    }

    public function get($key)
    {
        return $this->redis_connect->get($this->addPrefix($key));
    }

    public function command($method, array $parameters = [])
    {
        return $this->redis_connect->command($method, $parameters);
    }

    public function exists($key)
    {
        return $this->redis_connect->command('exists', [$this->addPrefix($key)]);
    }

    public function del($key)
    {
        return $this->redis_connect->command('del', [$this->addPrefix($key)]);
    }
}
